package com.longcai.piaojia.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.longcai.piaojia.BaseApplication;
import com.longcai.piaojia.activity.GoodsInfoActivity;
import com.longcai.piaojia.activity.MessageActivity;
import com.longcai.piaojia.activity.NavigationActivity;
import com.longcai.piaojia.db.BaseTable;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;

import cn.jpush.android.api.JPushInterface;

/**
 * 自定义接收器
 * <p/>
 * 如果不定义这个 Receiver，则：
 * 1) 默认用户会打开主界面
 * 2) 接收不到自定义消息
 */
public class JPushReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();

//        receivingNotification(context, bundle);
//
//        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
//            String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
//        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
//        }else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
//            int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
//
//        } else

        if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {

            Log.v("maning", bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID) + "");

            SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
            String date = sDateFormat.format(new java.util.Date());
            BaseTable.BaseA baseA = new BaseTable.BaseA();
            baseA.push_id = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
            baseA.message = bundle.getString(JPushInterface.EXTRA_ALERT);
            baseA.time = date;

            BaseApplication.baseDB.baseTable.insert(baseA);

            context.sendBroadcast(new Intent(MessageActivity.MESSAGE_RECEIVER));

        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
           /* Intent i = new Intent(context, NavigationActivity.class);
            bundle.putInt("index", 1);
            i.putExtras(bundle);

            //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            context.startActivity(i);*/

            Log.d(intent.getAction(), "adsklfjjjjjjjjjj;j;j;j;j;j;;");

            String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
            Log.d("maning", " title : " + title);
            String message = bundle.getString(JPushInterface.EXTRA_ALERT);
            Log.d("maning", "message : " + message);
            String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
            Log.d("maning", "extras : " + extras);
            int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
            String string = null;
            try {
                JSONObject obj = null;
                obj = new JSONObject(extras);
                string = obj.optString("pid");
                Log.d("11111", string);
            } catch (JSONException e) {
                e.printStackTrace();
            }

            if (TextUtils.isEmpty(string)) {


            /*    Intent i = new Intent(context, NavigationActivity.class);
//            bundle.putInt("id", "20");
                String regId = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_ID);
             //   bundle.putString("id", "1");
                i.putExtras(bundle);

                //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                context.startActivity(i);
            Intent i = new Intent(context, NavigationActivity.class);
                context.startActivity(i); */
                String regId = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_ID);

                Intent i = new Intent(context, NavigationActivity.class);
                bundle.putString("index", regId);
                i.putExtras(bundle);

                //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                context.startActivity(i);


            } else {
                Intent i = new Intent(context, GoodsInfoActivity.class);
//            bundle.putInt("id", "20");
                String regId = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_ID);
                bundle.putString("id", string);
                i.putExtras(bundle);

                //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                context.startActivity(i);
            }


/*

            StringBuilder sb = new StringBuilder();
            for (String key : bundle.keySet()) {
                if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
                    sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));
                    Log.d("keykeykeykeykeykey",key);


                } else if (key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)) {
                    sb.append("\nkey:" + key + ", value:" + bundle.getBoolean(key));
                    Log.d("keykeykeykeykeykey",key);
                } else {
                    sb.append("\nkey:" + key + ", value:" + bundle.getString(key));
                    Log.d("keykeykeykeykeykey",key);
                }
            }

*/
/*

            Log.d(notifactionId + "", "000000000000000000000");


            Intent i = new Intent(context, GoodsInfoActivity.class);
//            bundle.putInt("id", "20");
            String regId = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_ID);
            bundle.putString("id", "20");
            i.putExtras(bundle);

            //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            context.startActivity(i);*/
        }
//        else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
//            //在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码，比如打开新的Activity， 打开一个网页等..
//
//        } else if(JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
//            boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
//        } else {
//        }
    }

//    private void receivingNotification(Context context, Bundle bundle){
//        String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
//        Log.d("maning", " title : " + title);
//        String message = bundle.getString(JPushInterface.EXTRA_ALERT);
//        Log.d("maning", "message : " + message);
//        String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
//        Log.d("maning", "extras : " + extras);
//    }

    // 打印所有的 intent extra 数据
    private static String printBundle(Bundle bundle) {
        StringBuilder sb = new StringBuilder();
        for (String key : bundle.keySet()) {
            if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
                sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));
            } else if (key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)) {
                sb.append("\nkey:" + key + ", value:" + bundle.getBoolean(key));
            } else {
                sb.append("\nkey:" + key + ", value:" + bundle.getString(key));
            }
        }
        return sb.toString();
    }

}
